package ee.maoinvest.frontend.controller.eventfinders;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

public class ElectricityMetersMenuEventFinder implements EventFinder
{
	private final String EMPTY_STRING = "";
	private Logger logger = Logger.getLogger(ElectricityMetersMenuEventFinder.class);
	
	@Override
	public String findEvent(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		String event = "undefined";
		
		try 
		{
			String mode = request.getParameter("mode");
			String submode = request.getParameter("submode");
			String formSubmitted = request.getParameter("formSubmitted");
			// forms, with id-s for replace or edit
			String localTerritoryElectricityMeterIDToReplaceOrDelete = request.getParameter("localTerritoryElectricityMeterIDToReplaceOrDelete");
			String outerTerritoryElectricityMeterIDToReplaceOrDelete = request.getParameter("outerTerritoryElectricityMeterIDToReplaceOrDelete");
			String substationMainElectricityMeterIDToReplaceOrDelete = request.getParameter("substationMainElectricityMeterIDToReplaceOrDelete");
			String localTerritoryElectricityMeterIDWhichParentMeterToReplace = request.getParameter("localTerritoryElectricityMeterIDWhichParentMeterToReplace");
			
			if (mode.equals("administerElectricityMetersPage") && submode != null && !submode.equals(EMPTY_STRING))
			{
				event = "undefined";
				
				if (submode.equals("allElectricityMetersPage"))
				{
					event = "allElectricityMetersPage";
				}
				
				
				if (submode.equals("deleteElectricityMeterPage"))
				{
					event = "deleteElectricityMeterPage";
				}
				
				
				if (submode.equals("insertSubstationMainElectricityMeterPage"))
				{
					event = "insertSubstationMainElectricityMeterPage";
				}
				
				if (submode.equals("insertLocalTerritoryElectricityMeterPage"))
				{
					event = "insertLocalTerritoryElectricityMeterPage";
				}
				
				if (submode.equals("insertOuterTerritoryElectricityMeterPage"))
				{
					event = "insertOuterTerritoryElectricityMeterPage";
				}
				
				
				if (submode.equals("replaceOrDeleteSubstationMainElectricityMeterPage")
						&& substationMainElectricityMeterIDToReplaceOrDelete != null
						&& !substationMainElectricityMeterIDToReplaceOrDelete.equals(""))
				{
					event = "replaceOrDeleteSubstationMainElectricityMeterPage";
				}
				
				if (submode.equals("replaceOrDeleteLocalTerritoryElectricityMeterPage")
						&& localTerritoryElectricityMeterIDToReplaceOrDelete != null
						&& !localTerritoryElectricityMeterIDToReplaceOrDelete.equals(""))
				{
					event = "replaceOrDeleteLocalTerritoryElectricityMeterPage";
				}
				
				if (submode.equals("replaceOrDeleteOuterTerritoryElectricityMeterPage")
						&& outerTerritoryElectricityMeterIDToReplaceOrDelete != null
						&& !outerTerritoryElectricityMeterIDToReplaceOrDelete.equals(""))
				{
					event = "replaceOrDeleteOuterTerritoryElectricityMeterPage";
				}
				
				
				
				if (submode.equals("changeLocalTerritoryElectricityMeterParentMeterPage")
						&& localTerritoryElectricityMeterIDWhichParentMeterToReplace != null
						&& !localTerritoryElectricityMeterIDWhichParentMeterToReplace.equals(""))
				{
					event = "changeLocalTerritoryElectricityMeterParentMeterPage";
				}
				
				
				if (submode.equals("replaceOrDeleteSubstationMainElectricityMeterPage"))
				{
					event = "replaceOrDeleteSubstationMainElectricityMeterPage";
				}
				
				if (submode.equals("replaceOrDeleteLocalTerritoryElectricityMeterPage"))
				{
					event = "replaceOrDeleteLocalTerritoryElectricityMeterPage";
				}
				
				if (submode.equals("replaceOrDeleteOuterTerritoryElectricityMeterPage"))
				{
					event = "replaceOrDeleteOuterTerritoryElectricityMeterPage";
				}
				
				
				if (submode.equals("insertSubstationMainElectricityMeterPage")
						&& formSubmitted != null
						&& formSubmitted.equals("insertSubstationMainElectricityMeterForm"))
				{
					event = "insertSubstationMainElectricityMeterFormPostEvent";
				}
				
				if (submode.equals("insertLocalTerritoryElectricityMeterPage")
						&& formSubmitted != null
						&& formSubmitted.equals("insertLocalTerritoryElectricityMeterForm"))
				{
					event = "insertLocalTerritoryElectricityMeterFormPostEvent";
				}
				
				if (submode.equals("insertOuterTerritoryElectricityMeterPage")
						&& formSubmitted != null
						&& formSubmitted.equals("insertOuterTerritoryElectricityMeterForm"))
				{
					event = "insertOuterTerritoryElectricityMeterFormPostEvent";
				}
				
				
				
				if (submode.equals("replaceOrDeleteSubstationMainElectricityMeterPage")
						&& formSubmitted != null
						&& formSubmitted.equals("replaceOrDeleteSubstationMainElectricityMeterForm"))
				{
					event = "replaceOrDeleteSubstationMainElectricityMeterFormPostEvent";
				}
				
				if (submode.equals("replaceOrDeleteLocalTerritoryElectricityMeterPage")
						&& formSubmitted != null
						&& formSubmitted.equals("replaceOrDeleteLocalTerritoryElectricityMeterForm"))
				{
					event = "replaceOrDeleteLocalTerritoryElectricityMeterFormPostEvent";
				}
				
				if (submode.equals("replaceOrDeleteOuterTerritoryElectricityMeterPage")
						&& formSubmitted != null
						&& formSubmitted.equals("replaceOrDeleteOuterTerritoryElectricityMeterForm"))
				{
					event = "replaceOrDeleteOuterTerritoryElectricityMeterFormPostEvent";
				}
				
				
				if (submode.equals("changeLocalTerritoryElectricityMeterParentMeterPage")
						&& formSubmitted != null
						&& formSubmitted.equals("changeLocalTerritoryElectricityMeterParentMeterForm"))
				{
					event = "changeLocalTerritoryElectricityMeterParentMeterFormPostEvent";
				}
				
				if (submode.equals("deleteElectricityMeterPage")
						&& formSubmitted != null
						&& formSubmitted.equals("deleteElectricityMeterForm"))
				{
					event = "deleteElectricityMeterFormPostEvent";
				}
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return event;
	}
}
